package conf

import (
	"fmt"
	"io/ioutil"
	"log"

	"gopkg.in/yaml.v2"
)

var (
	DriverName string
	DbName     string
)

type Yaml struct {
	Dbdriver string `yaml:"dbdriver"`
	Host     string `yaml:"host"`
	User     string `yaml:"user"`
	Pwd      string `yaml:"pwd"`
	Dbname   string `yaml:"dbname"`
}

func init() {
	yamlFile, err := ioutil.ReadFile("conf/conf.yaml")
	if err != nil {
		fmt.Println(err.Error())
	}
	var c Yaml
	err = yaml.Unmarshal(yamlFile, &c)
	if err != nil {
		fmt.Println(err.Error())
	}
	log.Print("Debug confyaml:", c)
	DriverName = c.Dbdriver
	if c.Dbdriver == "mysql" {
		DbName = c.User + ":" + c.Pwd + "@tcp(" + c.Host + ")/" + c.Dbname + "?charset=utf8mb4"
	}
	if c.Dbdriver == "sqlite3" {
		DbName = c.Dbname
	}
}
